#region File Description
//-----------------------------------------------------------------------------
// SpriteFontControl.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System;
using log4net;
using System.Reflection;
using DC2010.Objects;
using DC2010;
using DC2010.Objects.Base;
using DC2010.Tools;
#endregion

namespace WinFormsGraphicsDevice
{
    /// <summary>
    /// Example control inherits from GraphicsDeviceControl, which allows it to
    /// render using a GraphicsDevice. This control shows how to use ContentManager
    /// inside a WinForms application. It loads a SpriteFont object through the
    /// ContentManager, then uses a SpriteBatch to draw text. The control is not
    /// animated, so it only redraws itself in response to WinForms paint messages.
    /// </summary>
    class DisplayMapControl
    {
        //ContentManager content;
      //  SpriteBatch spriteBatch;
    //    SpriteFont font;

        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    //    private Dictionary<string, Texture2D> contentTextures = new Dictionary<string, Texture2D>();

        int MapCoordinateX = 0;
        int MapCoordinateY = 0;

        private Point Origin { get; set; }
        private int BoxSize { get; set; }


        /// <summary>
        /// Initializes the control, creating the ContentManager
        /// and using it to load a SpriteFont.
        /// </summary>
        protected void Initialize()
        {
            //content = new ContentManager(Services, "Content");

            //spriteBatch = new SpriteBatch(GraphicsDevice);

            //font = content.Load<SpriteFont>("Arial");
         //   AddTexturesFromFolder(@"Gfx\Editor\Tiles\", GraphicsDevice);
           // AddTexturesFromFolder(@"Gfx\Editor\Cursors\", GraphicsDevice);

            Origin = new Point(0, 0);
            BoxSize = 16;

           // this.MouseMove += new System.Windows.Forms.MouseEventHandler(DisplayMapControl_MouseMove);
            //this.MouseClick += new System.Windows.Forms.MouseEventHandler(DisplayMapControl_MouseClick);
            
        }

        void DisplayMapControl_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            {
                try
                {
                    string typ = Editor.I.ActiveTileType;

                    switch (typ)
                    {
                        case "Clear":
                            {
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = null;
                                break;
                            }

                        case "Floor":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
								dt.Info.TileType = TileType.Floor;
                                dt.Info.CanHoldItems = true;
                                dt.Info.CanMonsterPass = true;
                                dt.Info.CanPlayerPass = true;
                                dt.Info.Id = string.Format("TILE_FLOOR_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "Wall":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.TileType = TileType.Wall;
                                dt.Info.CanHoldItems = true;
                                dt.Info.CanMonsterPass = false;
                                dt.Info.CanPlayerPass = false;
                                dt.Info.Id = string.Format("TILE_WALL_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "FakeWall":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.TileType = TileType.IllusionWall;
                                dt.Info.CanHoldItems = true;
                                dt.Info.CanMonsterPass = true;
                                dt.Info.CanPlayerPass = true;
                                dt.Info.Id = string.Format("TILE_WALL_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "Pit":
                            {
                                PitTile dt = new PitTile();
								dt.TileType = TileType.Pit;
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.IsOpen = true;
                                dt.IsIllusionary = false;
                                dt.IsConcealed = false;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PitClosed":
                            {
                                PitTile dt = new PitTile();
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.TileType = TileType.Pit;
								dt.IsOpen = false;
                                dt.IsIllusionary = false;
                                dt.IsConcealed = false;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PitIllusion":
                            {
                                PitTile dt = new PitTile();
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.TileType = TileType.Pit;
								dt.IsOpen = true;
                                dt.IsIllusionary = true;
                                dt.IsConcealed = false;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PitConcealed":
                            {
                                PitTile dt = new PitTile();
                                dt.Info.Id = string.Format("TILE_PIT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
								dt.TileType = TileType.Pit;
                                dt.IsOpen = true;
                                dt.IsIllusionary = false;
                                dt.IsConcealed = true;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "PreasurePad":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.Id = string.Format("TILE_PREASSURE_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
								dt.TileType = TileType.Floor;
                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;

                            }
                        case "PreasurePadInvisible":
                            {
                                ClearFloorTile dt = new ClearFloorTile();
                                dt.Info.Id = string.Format("TILE_PREASSURE_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
								dt.TileType = TileType.Floor;
                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;

                            }
                        case "DoorWE":
                            {
                                Door d = Root.I.DoorSystem.Dict["DOOR_WOODEN"];
                                Door dCloned = (Door)d.Clone();
                                dCloned.Info.Id = string.Format("DOOR_WOODEN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dCloned.Info.TemplateId = "DOOR_WOODEN";

                                DoorTile dt = new DoorTile();
                                dt.Info.Id = string.Format("TILE_DOOR_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
								dt.TileType = TileType.Door;
                                dt.DoorId = dCloned.Info.Id;

                                dt.Direction = DoorDirection.WE;


                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.DoorSystem.AddToSystem(dCloned, dCloned.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "DoorNS":
                            {
                                Door d = Root.I.DoorSystem.Dict["DOOR_WOODEN"];
                                Door dCloned = (Door)d.Clone();
                                dCloned.Info.Id = string.Format("DOOR_WOODEN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dCloned.Info.TemplateId = "DOOR_WOODEN";

                                DoorTile dt = new DoorTile();
                                dt.Info.Id = string.Format("TILE_DOOR_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                dt.Direction = DoorDirection.NS;
                                dt.DoorId = dCloned.Info.Id;
								dt.TileType = TileType.Door;

                                Root.I.TileSystem.AddToSystem(dt, dt.Info.Id, ObjectTarget.Instance);
                                Root.I.DoorSystem.AddToSystem(dCloned, dCloned.Info.Id, ObjectTarget.Instance);

                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = dt;
                                break;
                            }
                        case "StairsUPNS":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.NS;
                                st.Direction = StairsDirection.Up;
								st.TileType = TileType.Stairs;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsUPEW":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.EW;
                                st.Direction = StairsDirection.Up;
								st.TileType = TileType.Stairs;
								
                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsUPSN":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.SN;
                                st.Direction = StairsDirection.Up;
								st.TileType = TileType.Door;
								
                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsUPWE":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_UP_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.WE;
                                st.Direction = StairsDirection.Up;
								st.TileType = TileType.Door;
								
                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNNS":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.NS;
                                st.Direction = StairsDirection.Down;
								st.TileType = TileType.Door;
								
                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNEW":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.EW;
                                st.Direction = StairsDirection.Down;
								st.TileType = TileType.Door;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNSN":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.SN;
                                st.Direction = StairsDirection.Down;
								st.TileType = TileType.Door;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "StairsDOWNWE":
                            {
                                //stair base tile is not complete yet
                                StairsTile st = new StairsTile();
                                st.Info.Id = string.Format("TILE_STAIRS_DOWN_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                st.TileVector = TileVector.WE;
                                st.Direction = StairsDirection.Down;
								st.TileType = TileType.Door;

                                Root.I.TileSystem.AddToSystem(st, st.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = st;

                                break;
                            }
                        case "PartyStart":
                            {
                                Map m = Root.I.IS.GetMap(Editor.I.CurrentMapId);
                                //m.Info.StartPosition = new Microsoft.Xna.Framework.Point(MapCoordinateX, MapCoordinateY);
                                break;
                            }
                        case "TeleportBlue":
                            {
                                ClearFloorTile cft = new ClearFloorTile();
                                cft.Info.CanHoldItems = true;
                                cft.Info.CanMonsterPass = true;
                                cft.Info.CanPlayerPass = true;
								cft.TileType = TileType.Teleport;
                                cft.Info.Id = string.Format("TILE_TELEPORT_{0}_{1}_{2}", MapCoordinateX, MapCoordinateY, Editor.I.CurrentMapId.ToUpper());
                                cft.Info.OnEnterEffects.Add(new SimpleEffect("I_TELEPORT", 1, 1, 0, 0, 0, Editor.I.CurrentMapId, null, null, null, null));
                                cft.Info.OnEnterEffects.Add(new SimpleEffect("I_PLAY_SOUND", 0, 0, 0, 0, 0, "teleport", null, null, null, null));

                                Root.I.TileSystem.AddToSystem(cft, cft.Info.Id, ObjectTarget.Instance);
                                Root.I.IS.GetMap(Editor.I.CurrentMapId)[MapCoordinateX, MapCoordinateY] = cft;
                                break;

                            }

                    }
                }
                catch (Exception ex)
                {
                    int a = 9;
                }
            }

            //Refresh();
        }

        void DisplayMapControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            MapCoordinateX = ((e.X - Origin.X) / BoxSize);
            MapCoordinateY = ((e.Y - Origin.Y) / BoxSize);

            //this.Parent.Parent.Parent.Parent.Text = string.Format("{0:00} : {1:00}", MapCoordinateX, MapCoordinateY);

        }

        Map m = null;

        #region Handle textures
        //private void AddTexturesFromFolder(string Folder, GraphicsDevice gd)
        //{
        //    DirectoryInfo di = new DirectoryInfo(Folder);
        //    if (di.Exists)
        //    {
        //        FileInfo[] fiField = di.GetFiles();
        //        foreach (FileInfo fi in fiField)
        //        {
        //            if ((fi.Extension == ".png") || (fi.Extension == ".bmp"))
        //            {
        //                string h = fi.FullName.Replace(Environment.CurrentDirectory, "");
        //                h = h.Replace(fi.Extension, "");
        //                h = h.Replace("\\Gfx\\", "");
        //                this.AddTexture(h, Texture2D.FromFile(gd, fi.FullName));
        //                log.Info("Added texture: "+ h);  
        //            }
        //        }
        //    }

        //}

        //public Texture2D this[string textureId]
        //{
        //    get
        //    {
        //        if (contentTextures.ContainsKey(textureId))
        //            return contentTextures[textureId];

        //        return null;
        //    }
        //}

        //public void AddTexture(string Key, Texture2D tex)
        //{
        //    if (!contentTextures.ContainsKey(Key))
        //        contentTextures.Add(Key, tex);
        //}

        #endregion

        /// <summary>
        /// Disposes the control, unloading the ContentManager.
        /// </summary>
        protected void Dispose(bool disposing)
        {
            if (disposing)
            {
              //  content.Unload();
            }

            //base.Dispose(disposing);
        }

        /// <summary>
        /// Draws the control, using SpriteBatch and SpriteFont.
        /// </summary>
        protected void Draw()
        {
            const string message = "Hello, World!\n" +
                                   "\n" +
                                   "I'm an XNA Framework GraphicsDevice,\n" +
                                   "running inside a WinForms application.\n" +
                                   "\n" +
                                   "This text is drawn using SpriteBatch,\n" +
                                   "with a SpriteFont that was loaded\n" +
                                   "through the ContentManager.\n" +
                                   "\n" +
                                   "The pane to my right contains a\n" +
                                   "spinning 3D triangle.";

          //  GraphicsDevice.Clear(Color.Black);

            //spriteBatch.Begin();

            m = Root.I.IS.GetMap(Editor.I.CurrentMapId);
//            if (m!=null)
  //              DrawMap(spriteBatch, new Point(0,0), 16, m);

    //        DrawCursor(spriteBatch);

            //spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White);
       //     spriteBatch.End();
        }

        //private void DrawCursor(SpriteBatch spriteBatch)
        //{
            
        //}

        //private void DrawMap(SpriteBatch spriteBatch, Point origin, int boxsize, Map m)
        //{
        //    for (int x = 0; x <= m.X; x++)
        //    {
        //        for (int y = 0; y <= m.Y; y++)
        //        {
        //            try
        //            {
        //                Tile t = m[x, y];
        //                if (t == null) break;

        //                if (t != null)
        //                {
        //                    // draw floor
        //                    if (t is ClearFloorTile)
        //                    {
        //                        if (!t.Info.IsWall)
        //                        {
        //                            bool bItIsTeleport = false;

        //                            //check for teleport effect
        //                            foreach (SimpleEffect se in t.Info.OnEnterEffects)
        //                            {
        //                                if (se.Id == "I_TELEPORT") bItIsTeleport = true;
        //                            }

        //                            if (!bItIsTeleport)
        //                                spriteBatch.Draw(this["Editor\\Tiles\\Floor"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize,boxsize), Color.White); 
        //                            else
        //                                spriteBatch.Draw(this["Editor\\Tiles\\TeleportBlue"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize,boxsize), Color.White); 
        //                        }

        //                        if (t.Info.IsWall)
        //                        {
        //                            //is it fake?
        //                            if (t.Info.CanPlayerPass)
        //                                spriteBatch.Draw(this["Editor\\Tiles\\FakeWall"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize,boxsize), Color.White); 
        //                            else
        //                                spriteBatch.Draw(this["Editor\\Tiles\\Wall"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);
        //                        }
        //                    }

        //                    //draw pit
        //                    if (t is PitTile)
        //                    {
        //                        PitTile pt = (PitTile)t;

        //                        if (pt.IsOpen)
        //                            spriteBatch.Draw(this["Editor\\Tiles\\Pit"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if (!pt.IsOpen)
        //                            spriteBatch.Draw(this["Editor\\Tiles\\PitClosed"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if (pt.IsConcealed)
        //                            spriteBatch.Draw(this["Editor\\Tiles\\PitConcealed"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if (pt.IsIllusionary)
        //                            spriteBatch.Draw(this["Editor\\Tiles\\PitIllusion"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);
        //                    }


        //                    if (t is StairsTile)
        //                    {
        //                        StairsTile st = (StairsTile)t;

        //                        StairsDirection sd = st.Direction;
        //                        TileVector tv = st.TileVector;

        //                        if ((sd == StairsDirection.Up) && (tv == TileVector.EW))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsEW"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if ((sd == StairsDirection.Up) && (tv == TileVector.NS))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsNS"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if ((sd == StairsDirection.Up) && (tv == TileVector.SN))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsSN"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if ((sd == StairsDirection.Up) && (tv == TileVector.WE))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsWE"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if ((sd == StairsDirection.Down) && (tv == TileVector.EW))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsWE"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if ((sd == StairsDirection.Down) && (tv == TileVector.NS))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsSN"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if ((sd == StairsDirection.Down) && (tv == TileVector.SN))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsNS"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                        if ((sd == StairsDirection.Down) && (tv == TileVector.WE))
        //                            spriteBatch.Draw(this["Editor\\Tiles\\StairsEW"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //                    }

        //                    //draw door
        //                    if (t is DoorTile)
        //                    {
        //                        DoorDirection dd = DoorDirection.NS;

        //                        if (t is DoorTile) dd = ((DoorTile)t).Direction;

        //                        if (dd == DoorDirection.NS)
        //                        {
        //                            spriteBatch.Draw(this["Editor\\Tiles\\DoorNS"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);
        //                        }

        //                        if (dd == DoorDirection.WE)
        //                        {
        //                            spriteBatch.Draw(this["Editor\\Tiles\\DoorWE"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);
        //                        }
        //                    }

        //                    if ((m.Info.StartPosition.X == x) && (m.Info.StartPosition.Y == y))
        //                    {
        //                        spriteBatch.Draw(this["Editor\\Tiles\\PartyStart"], new Rectangle(x * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);
        //                    }

        //                }
        //                spriteBatch.Draw(this["Editor\\Tiles\\Wall"], new Rectangle((m.X + 1) * boxsize + origin.X, y * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //            }
        //            catch (Exception ex)
        //            {
        //                log.Error("", ex);  
        //            }
        //            spriteBatch.Draw(this["Editor\\Tiles\\Wall"], new Rectangle(x * boxsize + origin.X, (m.Y + 1) * boxsize + origin.Y, boxsize, boxsize), Color.White);

        //        }
        //    }

        //    Tile tx = m[MapCoordinateX, MapCoordinateY];
        //    DrawAssociations(spriteBatch, tx, MapCoordinateX, MapCoordinateY, origin, boxsize);

        //}

        //public void DrawAssociations(SpriteBatch spriteBatch, Tile t, int x, int y, Point Origin, int BoxSize)
        //{
        //    //log.Debug("DrawAssociations: " + x.ToString() + ":" + y.ToString() + "-" + Origin.ToString());
        //    foreach (string s in t.Info.LinkedObjects)
        //    {
        //        // TODO
        //    }

        //    try
        //    {
        //        foreach (DungeonPlacement dp in t.Info.Items)
        //        {
        //            BaseObject bo = Root.I.IS.GetLinkedObject(dp.ObjectId);

        //            if (bo is Item)
        //            {
        //                Item i = (Item)bo;
        //                foreach (string s in i.Info.LinkedObjects)
        //                {
        //                    DungeonPlacement dpLinked = Root.I.GameSystem.FindLinkedObjectLocation(s);

        //                    if (dpLinked != null)
        //                    {
        //                        Point pFrom = new Point(0, 0);
        //                        Point pTo = new Point(0, 0);

        //                        if (dp.ObjectPosition == ObjectPosition.East)
        //                        {
        //                            pFrom = new Point((x * BoxSize) + Origin.X + 1 * BoxSize, (y * BoxSize) + Origin.Y + BoxSize / 2);
        //                        }

        //                        if (dp.ObjectPosition == ObjectPosition.South)
        //                        {
        //                            pFrom = new Point((x * BoxSize) + Origin.X + 1 * BoxSize - BoxSize / 2, (y * BoxSize) + Origin.Y + BoxSize);
        //                        }

        //                        pFrom = new Point((x * BoxSize) + Origin.X -3, (y * BoxSize) + Origin.Y -3);

        //                        //if (dpLinked.ObjectPosition == ObjectPosition.Center)
        //                        {
        //                            pTo = (Point)dpLinked.Tag;
        //                            pTo.X = pTo.X * BoxSize + Origin.X-3;
        //                            pTo.Y = pTo.Y * BoxSize + Origin.Y-3;
        //                        }

        //                        int xx = pFrom.X;
        //                        int yy = pFrom.Y;
        //                        spriteBatch.Draw(this["Editor\\Cursors\\Common"], new Rectangle(xx, yy, 22, 22), Color.White);

        //                        xx = pTo.X;
        //                        yy = pTo.Y;
        //                        spriteBatch.Draw(this["Editor\\Cursors\\Common"], new Rectangle(xx, yy, 22, 22), Color.White);
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error("", ex);
        //    }





        //}


    }
}
